package com.ds.esd.bpm.plugins.attachment;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAction;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.tool.ui.enums.event.ActionTypeEnum;
import com.ds.vfs.FileInfo;
import com.ds.vfs.ct.CtVfsFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path = "/bpm/plugins/attachment/")
public class AttachMentCMDAction {
    @MethodChinaName(cname = "下载")
    @CustomAnnotation(imageClass = "spafont spa-icon-select1")
    @RequestMapping(value = {"download"}, method = {RequestMethod.POST})
    @APIEventAnnotation(
            bindAction = {@CustomAction(method = "call", type = ActionTypeEnum.other, target = "callback", args = {"{xui.downLoad()}", "undefined", "undefined", "{args[1].path}"}, _return = false)}
    )
    @ResponseBody
    public ResultModel<Boolean> download(String activityInstId, String activityInstHistoryId, String path) {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }

    @MethodChinaName(cname = "删除附件")
    @RequestMapping(method = RequestMethod.POST, value = "delAttacheMent")
    @CustomAnnotation(imageClass = "fa fa-lg fa-close")
    @APIEventAnnotation(callback = {CustomCallBack.Reload})
    public @ResponseBody
    ResultModel<Boolean> delAttacheMent(String path) {
        ResultModel<Boolean> resultModel = new ResultModel<Boolean>();
        String[] pathArr = StringUtility.split(path, ";");
        List<FileInfo> fileInfos = new ArrayList<FileInfo>();
        try {
            for (String filepath : pathArr) {
                FileInfo file = CtVfsFactory.getCtVfsService().getFileByPath(filepath);
                CtVfsFactory.getCtVfsService().deleteFile(file.getID());
            }
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;

    }

}

